﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Graphics.PackedVector;


namespace BloxGin.Graphics
{
    public struct VertexBlock : IVertexType
    {
        public static readonly VertexDeclaration VertexDeclaration;

        public ByteVector4 Position;
      //  public Color Color;
        public HalfVector2 TextureCoordinate;
        public Vector3 Normal;

        VertexDeclaration IVertexType.VertexDeclaration
        {
            get { return VertexDeclaration; }
        }

        static VertexBlock()
        {
            VertexElement[] elements = new VertexElement[]
			{
				new VertexElement(0, VertexElementFormat.Byte4, VertexElementUsage.Position, 0),
				//new VertexElement(4, VertexElementFormat.Color, VertexElementUsage.Color, 0),
				new VertexElement(4, VertexElementFormat.HalfVector2, VertexElementUsage.TextureCoordinate, 0),
                new VertexElement(8, VertexElementFormat.Vector3, VertexElementUsage.Normal, 0)
			};

            VertexDeclaration = new VertexDeclaration(elements) { Name = "VertexBlock.VertexDeclaration" };
        }

        /// <summary>
        /// Create a new VertexBlock
        /// </summary>
        /// <param name="position">The position in chunk space</param>
        /// <param name="normal">The normal of the vertex</param>
        /// <param name="textureCoordinate">The texture co-ordinate of the vertex</param>
        public VertexBlock(ByteVector4 position,Vector3 normal, HalfVector2 textureCoordinate)
        {

            this.Normal = normal;
            this.Position = position;
           // this.Color = color;
            this.TextureCoordinate = textureCoordinate;
        }

        public static bool operator ==(VertexBlock left, VertexBlock right)
        {
            return ((left.Position == right.Position) && /*(left.Color == right.Color)) &&*/
                (left.TextureCoordinate == right.TextureCoordinate));
        }

        public static bool operator !=(VertexBlock left, VertexBlock right)
        {
            return !(left == right);
        }

        public override bool Equals(object obj)
        {
            if (obj == null)
            {
                return false;
            }

            if (obj.GetType() != base.GetType())
            {
                return false;
            }

            return (this == ((VertexBlock)obj));
        }

        public override string ToString()
        {
            return String.Format("{{Position:{0} TextureCoordinate:{2}}}",
                this.Position, this.TextureCoordinate);
        }
    }
}